#include<iostream>
#include<unistd.h>
#include<signal.h>
#include<sys/types.h>
#include<sys/wait.h>

using namespace std;

void PrintPending(sigset_t &pending)
{
    for(int signo = 31; signo >= 1; signo--)
    {
        if(sigismember(&pending, signo))
        {
            cout << "1"; 
        }
        else 
        {
            cout << "0"; 
        }
    }
    cout << "\n\n"; 
}
void handler(int signo)
{
    cout << "catch a signo: " << signo << endl; 
}
int main()
{
    sigset_t bset, oset;
    sigemptyset(&bset);
    sigemptyset(&oset);
    for(int i = 1; i <= 31; i++)
    {
        sigaddset(&bset, i); 
    }
    sigprocmask(SIG_SETMASK, &bset, &oset);
    sigset_t pending;
    while(true)
    {
        int n = sigpending(&pending);
        if(n < 0)
            continue; 
        PrintPending(pending);
        sleep(1); 
    }

//    signal(2, handler);
//    sigset_t bset, oset; 
//    sigemptyset(&bset);
//    sigemptyset(&oset);
//    sigaddset(&bset, 2); 
//
//    sigset_t pending;
//    int cnt = 0;
//    while(true)
//    {
//        int n = sigpending(&pending);
//        if(n < 0)
//            continue; 
//        PrintPending(pending); 
//        sleep(1); 
//        cnt++; 
//
//        if(cnt == 20)
//        {
//            cout << "unblock 2 signo" << endl; 
//            sigprocmask(SIG_SETMASK, &oset, nullptr); 
//        }
//    }
    return 0;
}


//int main()
//{
//    signal(2, SIG_DFL);
//
//    while(1)
//    {
//        cout << "hello signal" << endl;
//        sleep(1); 
//    }
//
//
////    int a = 10;
////    int b = 0;
////    a /= b;
////    cout << "a = " << a << endl; 
////    pid_t id = fork();
////
////    if(id == 0)
////    {
////        int cnt = 500; 
////        while(cnt)
////        {
////            cout << "I am a child process, pid: " << getpid() << "cnt: " << cnt << endl; 
////            sleep(1); 
////            cnt--; 
////        }
////        exit(0); 
////    }
////
////    int status = 0;
////    pid_t rid = waitpid(id, &status, 0);
////    if(rid == id)
////    {
////        cout << "child quit info, rid: " << rid << "exit code: " << ((status>>8)&0xFF) << "exit signal: " 
////            << "core dump: " << ((status>>7)&1) << endl; 
////    }
//    return 0;
//}
